/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.reservacion.vista;

import basedatos.OperacionesBD;
import basedatos.ReservacionBD;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import modelo.Pyme;
import modulos.agenda.vista.iFrmAgenda;
import modulos.usuario.modelo.Usuario;
import modulos.paciente.modelo.Paciente;
import modulos.reportes.consulta.ConsultaImprimir;
import modulos.reservacion.modelo.Hora;
import modulos.reservacion.modelo.Reservacion;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import vista.frmPrincipal;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class iFrmConsultaPaciente extends javax.swing.JInternalFrame {

    private DefaultTableModel modeloTablaReservaciones = new DefaultTableModel();
    private ArrayList<Reservacion> listaReservaciones = new ArrayList<Reservacion>();
    private ArrayList<Usuario> listaUsuarios = new ArrayList<Usuario>();
    private Usuario usuario = null;
    private Paciente paciente = null;
    private double valorTotal;

    /**
     * Creates new form iFrmConsultaPaciente
     */
    public iFrmConsultaPaciente() {
        modeloTablaReservaciones.addColumn("Nro.");
        modeloTablaReservaciones.addColumn("Especialista");
        modeloTablaReservaciones.addColumn("N° HC");
        modeloTablaReservaciones.addColumn("fecha");
        modeloTablaReservaciones.addColumn("hora");
        modeloTablaReservaciones.addColumn("Servicio");
        modeloTablaReservaciones.addColumn("Valor");
        modeloTablaReservaciones.addColumn("estado");
        initComponents();
        listarMedicos();
    }

    private void listarReservaciones()//muestra las reservaciones del medico seleccionado
    {
        valorTotal = 0;
        int c = modeloTablaReservaciones.getRowCount();
        for (int i = 0; i < c; i++) {
            modeloTablaReservaciones.removeRow(0);
        }
        int cont = 0;
        DateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        for (Reservacion r : listaReservaciones) {
            cont++;
            valorTotal += r.getServicio().getCosto();
            Object[] fila = {
                cont,
                r.getUsuario(),
                r.getPaciente().getNumeroHC(),
                formato.format(r.getFecha()),
                new Hora(r.getHora()),
                r.getServicio(),
                r.getServicio().getCosto(),
                r.getEstado()
            };
            modeloTablaReservaciones.addRow(fila);
        }
        txtValorTotal.setText(valorTotal+" $");
    }

    private void listarMedicos() {
        ArrayList<Usuario> aux = (ArrayList<Usuario>) OperacionesBD.listar("Usuario");
        for (Usuario obj : aux) {
            if (((Usuario) obj).getCuentaUsr().getTipo().equalsIgnoreCase("MEDICO")) {
                listaUsuarios.add(obj);
            }
        }
        for (Usuario medico : listaUsuarios) {
            cmbEspecialista.addItem((medico));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrolpReserva = new javax.swing.JScrollPane();
        tblReservaciones = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        jPanel1 = new javax.swing.JPanel();
        cbxPaciente = new javax.swing.JCheckBox();
        cbxEspecialista = new javax.swing.JCheckBox();
        cbxTodos = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        fechaDesde = new com.toedter.calendar.JDateChooser();
        fechaHasta = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbEspecialista = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtPaciente = new javax.swing.JTextField();
        btnCargarPaciente = new javax.swing.JButton();
        btnListar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        txtValorTotal = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("LISTADO DE CONSULTAS");

        tblReservaciones.setFont(new java.awt.Font("Ubuntu", 0, 14)); // NOI18N
        tblReservaciones.setModel(modeloTablaReservaciones);
        tblReservaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblReservacionesMouseReleased(evt);
            }
        });
        tblReservaciones.addHierarchyListener(new java.awt.event.HierarchyListener() {
            public void hierarchyChanged(java.awt.event.HierarchyEvent evt) {
                tblReservacionesHierarchyChanged(evt);
            }
        });
        tblReservaciones.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblReservacionesKeyReleased(evt);
            }
        });
        scrolpReserva.setViewportView(tblReservaciones);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar Por:"));

        cbxPaciente.setSelected(true);
        cbxPaciente.setText("Paciente");
        cbxPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxPacienteActionPerformed(evt);
            }
        });

        cbxEspecialista.setSelected(true);
        cbxEspecialista.setText("Especialista");
        cbxEspecialista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxEspecialistaActionPerformed(evt);
            }
        });

        cbxTodos.setText("Todos");
        cbxTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cbxPaciente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbxEspecialista)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbxTodos))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxEspecialista)
                    .addComponent(cbxPaciente)
                    .addComponent(cbxTodos))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Fecha"));

        fechaDesde.setDate(new Date());

        fechaHasta.setDate(new Date());

        jLabel4.setText("Hasta:");

        jLabel3.setText("Desde: ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fechaDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(fechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fechaDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jLabel3)
            .addComponent(jLabel4)
            .addComponent(fechaHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Especialista :");

        cmbEspecialista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbEspecialistaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(cmbEspecialista, 0, 247, Short.MAX_VALUE)
                .addGap(19, 19, 19))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbEspecialista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Paciente: ");

        txtPaciente.setToolTipText("Ingrese la cedula del paciente");
        txtPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPacienteActionPerformed(evt);
            }
        });
        txtPaciente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPacienteKeyTyped(evt);
            }
        });

        btnCargarPaciente.setText("Cargar");
        btnCargarPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarPacienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                .addGap(14, 14, 14)
                .addComponent(btnCargarPaciente)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCargarPaciente))
                .addContainerGap())
        );

        btnListar.setText("Listar");
        btnListar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListarActionPerformed(evt);
            }
        });

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnImprimir.setFont(new java.awt.Font("DejaVu Sans", 0, 13)); // NOI18N
        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/printer1.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        txtValorTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N

        jLabel5.setText("Valor total de las consultas: ");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtValorTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtValorTotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnImprimir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(11, 11, 11)
                                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btnListar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addComponent(scrolpReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 779, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnListar)
                        .addGap(18, 18, 18)))
                .addComponent(scrolpReserva, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnCerrar)
                        .addComponent(btnImprimir))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblReservacionesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblReservacionesMouseReleased
//        int index = tblReservaciones.getSelectedRow();
//        if (index != -1) {
//            reservacion = listaReservaciones.get(index);
//            setReservacion(reservacion);
//            btnConsulta.setEnabled(true);
//            btnFinalizar.setEnabled(true);
//            rbtnNo.setEnabled(true);
//            rbtnSi.setEnabled(true);
//
//        }
    }//GEN-LAST:event_tblReservacionesMouseReleased

    private void tblReservacionesHierarchyChanged(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_tblReservacionesHierarchyChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_tblReservacionesHierarchyChanged

    private void tblReservacionesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblReservacionesKeyReleased
   }//GEN-LAST:event_tblReservacionesKeyReleased

    private void cbxEspecialistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxEspecialistaActionPerformed
        if (cbxEspecialista.isSelected()) {
            cmbEspecialista.setEnabled(true);
        } else {
            cmbEspecialista.setEnabled(false);
        }
    }//GEN-LAST:event_cbxEspecialistaActionPerformed

    private void txtPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPacienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPacienteActionPerformed

    private void cbxPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxPacienteActionPerformed
        if (cbxPaciente.isSelected()) {
            txtPaciente.setEnabled(true);
            btnCargarPaciente.setEnabled(true);
        } else {
            txtPaciente.setEnabled(false);
            btnCargarPaciente.setEnabled(false);
        }
    }//GEN-LAST:event_cbxPacienteActionPerformed

    private void cbxTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxTodosActionPerformed
        if (cbxTodos.isSelected()) {
            cbxEspecialista.setEnabled(false);
            cbxPaciente.setEnabled(false);
            cmbEspecialista.setEnabled(false);
            txtPaciente.setEnabled(false);
            btnCargarPaciente.setEnabled(false);
        } else {
            cbxEspecialista.setEnabled(true);
            cbxPaciente.setEnabled(true);
            if (cbxEspecialista.isSelected()) {
                cmbEspecialista.setEnabled(true);
            }
            if (cbxPaciente.isSelected()) {
                txtPaciente.setEnabled(true);
                btnCargarPaciente.setEnabled(true);
            }
        }
    }//GEN-LAST:event_cbxTodosActionPerformed

    private void btncerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncerrarActionPerformed
        dispose();
    }//GEN-LAST:event_btncerrarActionPerformed

    private void btnListarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListarActionPerformed
        Date desde = fechaDesde.getDate();
        Date hasta = fechaHasta.getDate();
        if (desde != null && hasta != null) {
            if (hasta.after(desde)) {
                if (cbxTodos.isSelected()) {
                    listaReservaciones = (ArrayList<Reservacion>) ReservacionBD.listarPorFecha(desde, hasta);
                } else {
                    if (cbxPaciente.isSelected()) {
                        if (paciente != null) {
                            if (cbxEspecialista.isSelected()) {
                                if (usuario != null) {
                                    listaReservaciones = (ArrayList<Reservacion>) ReservacionBD.listarPorFecha(usuario, paciente, desde, hasta);
                                } else {
                                    JOptionPane.showMessageDialog(this, "Primero seleccione el especialista", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                                }
                            } else {
                                listaReservaciones = (ArrayList<Reservacion>) ReservacionBD.listarPorFecha(paciente, desde, hasta);
                            }
                        } else {
                            JOptionPane.showMessageDialog(this, "Primero carge el paciente respectivo", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                        }
                    } else {
                        if (cbxEspecialista.isSelected()) {
                            if (usuario != null) {
                                listaReservaciones = (ArrayList<Reservacion>) ReservacionBD.listarPorFecha(usuario, desde, hasta);
                            } else {
                                JOptionPane.showMessageDialog(this, "Primero seleccione el especialista", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                            }
                        } else {
                            JOptionPane.showMessageDialog(this, "Primero determine un criterio de busqueda", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "El intervalo de fechas seleccionado no es permitido", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione fechas validas", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }
        listarReservaciones();
    }//GEN-LAST:event_btnListarActionPerformed

    private void txtPacienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPacienteKeyTyped
        OperacionesVarias.ingresoNumCedula(evt, txtPaciente.getText());
    }//GEN-LAST:event_txtPacienteKeyTyped

    private void btnCargarPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarPacienteActionPerformed
        if (OperacionesBD.existe("Paciente", "cedula", txtPaciente.getText().trim())) {
            paciente = (Paciente) OperacionesBD.buscar("Paciente", "cedula", txtPaciente.getText().trim());
            if (paciente != null) {
                JOptionPane.showMessageDialog(this, "El paciente " + paciente + " ha sido cargado correctamente", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "Error al cargar el paciente vuelva a intentar", "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Paciente no existe en la base de datos", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnCargarPacienteActionPerformed

    private void cmbEspecialistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbEspecialistaActionPerformed
        int index = cmbEspecialista.getSelectedIndex();
        if (index != -1) {
            usuario = listaUsuarios.get(index);
        } else {
            usuario = null;
        }
    }//GEN-LAST:event_cmbEspecialistaActionPerformed
    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
        //Cargas Datos de Compañia
        ArrayList<Pyme> list = (ArrayList<Pyme>) OperacionesBD.listar("Pyme");
        Pyme pyme = null;
        if (!list.isEmpty()) {
            pyme = list.get(0);
        } else {
            pyme = new Pyme();
        }
        Map<String, Object> parametros = new HashMap<String, Object>();
        parametros.put("MEDICO", usuario + "");
        parametros.put("COMPANIA", pyme.getNombre());
        parametros.put("DIRECCION", pyme.getDireccion());
        parametros.put("TELEFONO", pyme.getTelefono());
        parametros.put("FAX", pyme.getFax());
        parametros.put("VALOR", valorTotal+"");

        try {

            JasperReport reporte = (JasperReport) JRLoader.loadObject(getClass().getResource("/modulos/reportes/consulta/Consulta.jasper"));
            JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JRBeanCollectionDataSource(datosImprimir()));
            JasperViewer vista = new JasperViewer(jasperPrint, false);
            vista.setSize(frmPrincipal.dskpPrincipal.size());
            vista.setVisible(true);
        } catch (JRException ex) {
            Logger.getLogger(iFrmAgenda.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_btnImprimirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCargarPaciente;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnListar;
    private javax.swing.JCheckBox cbxEspecialista;
    private javax.swing.JCheckBox cbxPaciente;
    private javax.swing.JCheckBox cbxTodos;
    private javax.swing.JComboBox cmbEspecialista;
    private com.toedter.calendar.JDateChooser fechaDesde;
    private com.toedter.calendar.JDateChooser fechaHasta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane scrolpReserva;
    private javax.swing.JTable tblReservaciones;
    private javax.swing.JTextField txtPaciente;
    private javax.swing.JLabel txtValorTotal;
    // End of variables declaration//GEN-END:variables

    private ArrayList<ConsultaImprimir> datosImprimir() {
        ArrayList<ConsultaImprimir> list = new ArrayList<ConsultaImprimir>();
        int cont = 1;
        for (Reservacion reservacion : listaReservaciones) {
            list.add(new ConsultaImprimir(reservacion, cont));
            cont++;
        }
        return list;
    }
}
